<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image round class="avatar" :src="userinfoList.photo" @click="changeheadFn" />
          <input type="file" ref="files" accept="image/*" v-show="false" @change="postimgFn">
        </template>
      </van-cell>
      <van-cell title="名称" is-link :value="userinfoList.name" @click="changenameFn" />
      <van-dialog v-model="show" title="修改昵称" show-cancel-button class="changename" :before-close="beforeCloseFn">
        <input type="text" v-fofo v-model="username" />
      </van-dialog>

      <van-cell title="生日" is-link :value="userinfoList.birthday" @click="birthClickFn" />
    </van-cell-group>
    <!-- 生日选择窗口 -->
    <van-popup v-model="dateShow" style="width: 100%; height: 50%;" position="bottom" round>
      <van-datetime-picker v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" @confirm="dateConfirmFn" @cancel="dateShow = false" />
    </van-popup>
  </div>

</template>

<script>
import { mapMutations } from 'vuex'
import moment from 'moment'
import { Notify } from 'vant'
import { userProfileAPI, updatePhotoAPI, updateProfileAPI } from '@/api/user.js'
export default {
  name: 'UserEdit',
  data() {
    return {
      userinfoList: {},
      show: false,
      username: '',
      minDate: new Date(1921, 0, 1), // 最小范围
      // new Date("1921-1-1") // 格式不同, 所以3个参数需要从0写月份
      maxDate: new Date(), // 最大范围(今日系统时间)
      currentDate: new Date(), // 默认当前显示日期
      dateShow: false // 时间选择器窗口出现/隐藏
    }
  },
  methods: {
    ...mapMutations(['updatePhoto', 'updateName']),
    changeheadFn() {
      this.$refs.files.click()
    },
    async postimgFn(ev) {
      const el = ev.target.files[0]
      // console.log(el)
      if (el.length === 0) return
      const fd = new FormData()
      fd.append('photo', el)
      const res = await updatePhotoAPI({
        fd
      })
      console.log(res)
      this.userinfoList.photo = res.data.data.photo
      this.updatePhoto(res.data.data.photo)
    },
    changenameFn() {
      this.show = true
      this.username = this.userinfoList.name
    },
    async beforeCloseFn(action, done) {
      if (action === 'confirm') {
        // action 为提交时用正则判断，如果通过就正常关闭弹窗
        if (/^[0-9a-zA-Z\u4e00-\u9fa5]{1,7}$/.test(this.username)) {
          await updateProfileAPI({
            uName: this.username
          })
          this.userinfoList.name = this.username
          this.updateName(this.username)
          done()
        } else {
          Notify({ type: 'danger', message: '姓名只能是1-7位中英文数字组合' })
          done(false)
        }
      } else {
        // action为取消时调用done(),正常关闭
        done()
      }
    },
    birthClickFn() {
      this.dateShow = true
      // 组件需要的是日期"对象", 显示和数据里日期是"字符串"格式
      // 日期字符串-->日期对象：new Date(日期字符串)
      this.currentDate = new Date(this.userinfoList.birthday)
    },
    // 生日时间选择事件
    async dateConfirmFn() {
      // 日期对象-->日期字符串：moment+format
      const time = moment(this.currentDate).format('YYYY-MM-DD')
      await updateProfileAPI({
        birthday: time
      })
      this.userinfoList.birthday = time
      this.dateShow = false // 关闭选择弹窗
    }
  },
  async created() {
    const res = await userProfileAPI()
    console.log(res)
    this.userinfoList = res.data.data
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
.changename {
  text-align: center;
  input {
    text-align: center;
    border: 0;
  }
}
</style>
